package com.yumeng.framework.core.base.controller;

import com.yumeng.common.data.constants.UIConstants;
import com.yumeng.common.exception.YumengException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * 基础Controller
 *
 * @author wxd
 * @date 2024/6/3 下午4:01
 */
public class BaseController {

    /**
     * 解析参数
     * @param result
     * @return
     */
    protected void checkValidationInfo(BindingResult result) {
        String errMsg = "";
        List<ObjectError> errorList = result.getAllErrors();
        for(ObjectError error : errorList){
            if (StringUtils.isNotBlank(errMsg)) {
                errMsg += ";";
            }
            errMsg += error.getDefaultMessage();
        }
        if (StringUtils.isNotBlank(errMsg)) {
            errMsg = "以下参数验证异常：" + errMsg;
            throw new YumengException(errMsg);
        }
    }

    /**
     * 验证参数
     * @param params
     */
    protected void checkParams(Object... params){
        for (Object para : params){
            String paraStr = String.valueOf(para);
            if (para== null || StringUtils.isBlank(paraStr) || "null".equals(paraStr)){
                throw new YumengException("参数缺失");
            }
        }
    }

    /**
     * 验证参数
     * @param params
     */
    protected void checkParamsForCombobox(Object... params){
        for (Object para : params){
            String paraStr = String.valueOf(para);
            if (para== null || StringUtils.isBlank(paraStr) || "null".equals(paraStr) || UIConstants.COMBBOX_TEMP_VALUE_STR.equals(paraStr)){
                throw new YumengException("参数缺失");
            }
        }
    }

}
